package com.ad.pet.app.photo.loader;

import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ad.pet.Logger;
import com.ad.pet.R;
import com.ad.pet.app.photo.model.ImageItem;
import com.ad.pet.common.data.utils.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by z on.
 * Des:
 * Date: 2017/3/31
 */

public class ImageAdapter extends BaseQuickAdapter<ImageItem, BaseViewHolder> {
    int max;
    private int selectCount = -1;

    public ImageAdapter(List<ImageItem> data, int max) {
        super(R.layout.item_photo_image_layout, data);
        this.max = max;
    }

    @Override
    protected void convert(BaseViewHolder helper, ImageItem item) {
        GlideUtil.load(mContext, item.getImagePath(), (ImageView) helper.getView(R.id.image));
        helper.setVisible(R.id.cb_selected, max > 0)
                .addOnClickListener(R.id.cb_selected)
                .setChecked(R.id.cb_selected, item.isSelected());
    }

    public int getSelectCount() {
        Logger.d("select:\n" + selectCount);
        if (selectCount == -1) {
            selectCount = getSelectedData().size();
        }
        return selectCount;
    }

    public void setSelectCount(int count) {
        this.selectCount = count;
    }

    public void setSelectCount(boolean b) {
        if (selectCount == -1) selectCount = 0;
        if (b) this.selectCount++;
        else this.selectCount--;
    }

    public ArrayList<ImageItem> getSelectedData() {
        ArrayList<ImageItem> selectData = new ArrayList<>();
        List<ImageItem> data = getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelected()) {
                selectData.add(data.get(i));
            }
        }
        return selectData;
    }

    public void resetSelectData() {
        if (selectCount == 0) return;
        for (int i = 0; i < getData().size(); i++) {
            ImageItem imageItem = getData().get(i);
            if (imageItem.isSelected()) {
                imageItem.setSelected(false);
                notifyItemChanged(i);
            }
        }
    }
}
